package com.qdlc.p2p.manage.action.additional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.ExperienceRuleService;
import com.qdlc.p2p.biz.service.UpRateService;
import com.qdlc.p2p.biz.service.lottery.LotteryService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.ExperienceRule;
import com.qdlc.p2p.dal.dto.Lottery;
import com.qdlc.p2p.dal.dto.RateRule;
import com.qdlc.p2p.dal.model.LotteryModel;

/**
 * Created by liaoxiangkai on 2015/12/1.
 */
public class ManageLotteryAction extends BaseAction<LotteryModel> implements ModelDriven<LotteryModel> {

    @Resource
    private LotteryService lotteryService;

    @Resource
    private UpRateService upRateService;

    @Resource
    private ExperienceRuleService experienceRuleService;

    private LotteryModel model = new LotteryModel();
    @Override
    public LotteryModel getModel() {
        return model;
    }

    private Map<String, Object> data;

    /**
     * 奖项管理
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/drawLottery/lotteryManage")
    public String lotteryManage() throws Exception {
        return "lotteryManage";
    }

    /**
     * 奖项列表
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/drawLottery/lotteryList")
    public void lotteryList() throws Exception {
        PageDataList<Lottery> dataList = lotteryService.getAllLottery(model);
        data = new HashMap<String, Object>();
        data.put("total", dataList.getPage().getTotal());
        data.put("rows", dataList.getList());
        printJson(getStringOfJpaObj(data));
    }

    /**
     * 添加奖项页面
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/drawLottery/lotteryAddPage")
    public String lotteryAddPage() throws Exception {
        return "lotteryAddPage";
    }

    /**
     * 添加奖项
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/drawLottery/lotteryDoAdd")
    public void lotteryDoAdd() throws Exception {
        int validDayType = paramInt("validDayType");
        //1：天，2：月
        if (validDayType == 2) {
            Date endTime = DateUtil.timeMonthManage(DateUtil.getNow(), model.getValidDay());
            model.setValidDay(DateUtil.daysBetween(DateUtil.getNow(), endTime));
        }
        //3:加息券，7：体验金，8抽中商品
        if (model.getType() == 3) {
            RateRule rateRule = upRateService.getRateRuleById(model.getItemId());
            if (null != rateRule)
                model.setValue(rateRule.getUpApr());
        } else if (model.getType() == 7) {
            ExperienceRule experienceRule = experienceRuleService.findById(model.getItemId());
            if (null != experienceRule)
                model.setValue(experienceRule.getAmount());
        }
        lotteryService.save(model);
        printSuccess();
    }

    /**
     * 修改奖项页面
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/drawLottery/lotteryEditPage")
    public String lotteryEditPage() throws Exception {
        Lottery lottery = lotteryService.findById(model.getId());
        request.setAttribute("lottery", lottery);
        return "lotteryEditPage";
    }

    /**
     * 修改奖项
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/drawLottery/lotteryDoEdit")
    public void lotteryDoEdit() throws Exception {
        int validDayType = paramInt("validDayType");
        //1：天，2：月
        if (validDayType == 2) {
            Date endTime = DateUtil.timeMonthManage(DateUtil.getNow(), model.getValidDay());
            model.setValidDay(DateUtil.daysBetween(DateUtil.getNow(), endTime));
        }
        //3:加息券，7：体验金，8抽中商品
        if (model.getType() == 3) {
            RateRule rateRule = upRateService.getRateRuleById(model.getItemId());
            if (null != rateRule)
                model.setValue(rateRule.getUpApr());
        } else if (model.getType() == 7) {
            ExperienceRule experienceRule = experienceRuleService.findById(model.getItemId());
            if (null != experienceRule)
                model.setValue(experienceRule.getAmount());
        }
        Lottery lottery = LotteryModel.prototype(model);
        lotteryService.update(lottery);
        printSuccess();
    }

    /**
     * 禁用奖项
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/drawLottery/lotteryDisable")
    public void lotteryDisable() throws Exception {
        Lottery lottery = lotteryService.findById(model.getId());
        lottery.setStatus(LotteryModel.STATUS_DISABLE);
        lotteryService.update(lottery);
        printSuccess();
    }

    /**
     * 启用奖项
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/additional/drawLottery/lotteryEnable")
    public void lotteryEnable() throws Exception {
        Lottery lottery = lotteryService.findById(model.getId());
        lottery.setStatus(LotteryModel.STATUS_ENABLE);
        lotteryService.update(lottery);
        printSuccess();
    }

}
